package persistence;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import model.Coordenada;
import model.RepresentacionDeDiagrama;




import com.thoughtworks.xstream.XStream;

/** 
 * Clase singleton para la serializacion, deserealizacion y busqueda de las coordenadas de los componentes de un diagrama 
 */
public class XMLCoordenadasDeComponentesDeDiagrama implements XMLPersistencia {
	private static XMLCoordenadasDeComponentesDeDiagrama instance = null;
	
	/*
	 * Directorio del usuario 
	 */
	private String directorio;
	
	public String getDirectorio(){
		return this.directorio;
	}
	
	public void setDirectorio(String directorio){
		this.directorio = directorio;
	}

	private XMLCoordenadasDeComponentesDeDiagrama() {
	}

	public static XMLCoordenadasDeComponentesDeDiagrama getInstance() {
		if (XMLCoordenadasDeComponentesDeDiagrama.instance == null) {
			XMLCoordenadasDeComponentesDeDiagrama.instance = new XMLCoordenadasDeComponentesDeDiagrama();
		}
		return XMLCoordenadasDeComponentesDeDiagrama.instance;
	}
	
	public void create(Object object) {
		try {
			XStream xstream = this.createXStream();
			//Guardo el objeto 
			FileOutputStream file = new FileOutputStream(this.filesDirectory() + this.persistingFileName(object));
			xstream.toXML(object, file);
			file.close();
		} catch (Exception e) {
			
		}
	}
	
	public void update(Object object) {
		//Guardo el objeto modificado
		this.create(object);
	}
	

	public void delete(Object object ) {
		File file = new File(this.filesDirectory() + this.persistingFileName(object));
		this.executeDelete(((RepresentacionDeDiagrama) object).getIdentificator(), file);
	}
	
	
	public void delete(String identificator) {
		File file = new File(this.filesDirectory() + identificator + "-rep.xml");
		this.executeDelete(identificator, file);
	}

	private void executeDelete(String identificator, File file) {
		if (file.delete()){
			System.out.println("La Coordenada " + identificator + " fue borrada exitosamente");
			System.out.println();
		}else{
			System.out.println("El Coordenada " + identificator + " no pudo ser borrada");
			System.out.println();
		}
	}
	
	
	public XStream createXStream() {
		XStream xstream = new XStream();
		xstream.alias("Coordenada", Coordenada.class);
		xstream.alias("RepresentacionDeDiagrama", RepresentacionDeDiagrama.class);
		xstream.alias("Elemento", String.class);
			
		return xstream;
	}
	
	
	public String persistingFileName(Object object) {
		return (((RepresentacionDeDiagrama) (object)).getIdentificator()) + "-rep" + ".xml";
	}
	
	
	public String filesDirectory() {
		return this.directorio + "\\Datos\\";
	}

	
	public Object findByIdentificator(String identificator) {
		RepresentacionDeDiagrama representacion = null;
		try {
			XStream xstream = this.createXStream();
			FileInputStream file = new FileInputStream(this.filesDirectory() + identificator + "-rep.xml");
			representacion = (RepresentacionDeDiagrama) xstream.fromXML(file);
			file.close();
		} catch (FileNotFoundException e) {
		} catch (IOException e){ 
		}
		return representacion;
	}

	
	public List<Object> findAll() {
		List<Object> listRepresentaciones = new ArrayList<Object>();
		try {
			XStream xstream = this.createXStream();
			RepresentacionDeDiagrama coordenada = null;
			File directory = new File(this.filesDirectory());
			String[] files = directory.list();
			for (int i = 0; i < files.length; ++i) {
				if(!files[i].equals(".svn")){
					FileInputStream file = new FileInputStream(this.filesDirectory() + files[i]);
					coordenada = (RepresentacionDeDiagrama) xstream.fromXML(file);
					listRepresentaciones.add(coordenada);
					file.close();
				}
			}
		} catch (FileNotFoundException e) {
			// TODO: mostrar mensaje agradable
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
		}
		return listRepresentaciones;
	}
}
